#include "comboboxdelegate.h"

ComboBoxDelegate::ComboBoxDelegate(QObject *parent) :
    QStyledItemDelegate(parent)
{
}

QWidget* ComboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const{

    QComboBox *box = new QComboBox(parent);
    box->addItems(m_items);
    return box;
}
void ComboBoxDelegate::setEditorData (QWidget *editor, const QModelIndex &index) const{
    QString value = index.model()->data(index, Qt::EditRole).toString();

    QComboBox* box = static_cast<QComboBox*>(editor);
    int indx = box->findText(value);
    box->setCurrentIndex(indx);
}

void ComboBoxDelegate::updateEditorGeometry (QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const{
    QRect rect = option.rect;
    editor->setGeometry(rect);
}

void ComboBoxDelegate::setModelData (QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const{
    QComboBox* box = static_cast<QComboBox*>(editor);

    QString value = box->currentText();
    model->setData(index,value,Qt::EditRole);
}

void ComboBoxDelegate::AddItem(const QString &item){
    m_items<<item;
}

void ComboBoxDelegate::Clear(){
    m_items.clear();
}

